<?php

namespace app\api\controller\Account;

//use addons\txim\TLSSigAPI;
//use app\admin\controller\userkanban\Customer;
//use app\admin\controller\KeywordsCompany;
use app\admin\model\Admin;
use app\admin\model\AdminBasicZoom;
use app\admin\model\AdminLog;
use app\admin\model\AuthGroupAccessZoom;
use app\admin\model\vactivities\Ticket;
use app\common\controller\Api;
//use app\common\controller\Callfunc;
use app\common\library\Ems;
use app\common\library\Iden;
use app\common\library\Sms;
use app\admin\model\Account;
use app\common\library\Token;
use Exception;
use fast\Random;
use think\Config;
use think\Cookie;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 测试接口
 */
class  Test extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    private $model = null;
    private $catemodel = null;
    private $tagsmodel = null;
    private $actmodel = null;
    private $ticket_model = null;
    protected $aact = null;
    protected $api_act = null;
    protected $mem_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\trades\assets\Order;//订单
        $this->actmodel = new \app\admin\model\vactivities\Activity;
        $this->catemodel = new \app\admin\model\superdata\Activitycategory;
        $this->tagsmodel = new \app\admin\model\superdata\Activitytags;
        $this->ticket_model = new \app\admin\model\vactivities\Ticket;
        $this->admin_model = new \app\admin\model\Admin;
        $this->mem_model = new \app\admin\model\member\Account;
    }


    /**
     * 会员中心
     */
    /*
     * 参与活动选项
     */
    public function order(){
//        $uInfo = $this->auth->getUserInfo();
//        if(empty($uInfo)){//未登录
//            $this->error(__('Please login first'), null, 401);
//        }
//        $admin_id = Iden::decrypt($uInfo['id']);
//        if(empty($admin_id)){
//            $this->error(__('获取用户参数出错'));
//        }

        $ids = $this->request->param("ids");//活动id
        if (!$ids) {
            $this->error(__('获取参数错误'),'',0);
        }
        $ids =  Iden::decrypt($ids);
        $row = $this->actmodel->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row = $row->toArray();
        //  print_r($row);die;
        if($row['status']!= $this->actmodel::ACTIVITY_STATUS_1){
            $this->error('活动状态有误');
        }
        $isticket = $row['isticket'];

        $tArr = [$this->actmodel::TICKET_STATUS_0,$this->actmodel::TICKET_STATUS_1,$this->actmodel::TICKET_STATUS_2];
        if(!in_array($isticket,$tArr)){
            $this->error(__('获取票务类型错误'),'',0);
        }

        switch ($isticket)
        {
            case 0://报名
                return $this->_enroll($ids);
                break;
            case 1://购买
                return $this->_buy($ids);
            case 2://直接加入活动
                return $this->_join($ids);
                break;
            default:
                return false;
                break;
        }

    }


    /*
     * $ids 活动 id
     * 报名
     */

    private function _enroll($ids){

//        $uInfo = $this->auth->getUserInfo();
//        if(empty($uInfo)){//未登录
//            $this->error(__('Please login first'), null, 401);
//        }
//        $admin_id = Iden::decrypt($uInfo['id']);
//        if(empty($admin_id)){
//            $this->error(__('获取用户参数出错'));
//        }
          $admin_id = 1;

        if(empty($ids)){
            $this->error(__('获取参数错误'), null, 0);
        }
        $row = $this->actmodel->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row = $row->toArray();
        $account_id = $row['admin_id'];
        $series_id = isset($row['series_id']) && $row['series_id'] > 0 ? 1:2;//1系列 2 活动
        $name = $row['name'];
        try{
            $params = [
                'orderid'=>trade_no(),
                'account_id'=>$account_id,//举办人
                'buyer_id'=>1,
                'activity_id'=>$ids,
                'amount'=>0,
                'paytype'=>0,
                'paytime'=>0,
                'state'=>$this->model::PAY_STATE_2,
                'createTime'=>time(),
                'ordertype'=>$series_id,
                'name'=>$name,
                'ticket_type'=>$this->actmodel::TICKET_STATUS_0,
                'sn'=>0
            ];
            //   print_r($params);die;
            $response =  $this->model->save($params);
            if(!$response){
                $this->error('生成订单失败');
            }
            //return $response;
        }catch (ValidateException $e) {
            $this->error($e->getMessage());
        } catch (PDOException $e) {
            $this->error($e->getMessage());
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $data['name'] = $name;
        $data['username'] = $this->mem_model::getUsrNameById($account_id);
        $this->success(__('报名成功'), $data, 200);
    }
    /*
     * 没试用的
     *
     */
    public function ar(){
        $array = array(
            'total'=>118,
            'info'=>[['number'=>'4','price'=>'89','id'=>'95'],
                     ['number'=>'3','price'=>'29','id'=>'30']],
        );

        $this->success(__('报名成功'), $array, 200);
       // print_r($array);
    }


}
